#include <stdio.h> 
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#include "sm3.h"
#include "common_func.h"
#include "core_time.h"

//BSP板级支持包所需全局变量
unsigned long UART_BASE = 0xbf000000;					//UART16550的虚地址
unsigned long CONFREG_TIMER_BASE = 0xbf20f100;			//CONFREG计数器的虚地址
unsigned long CONFREG_CLOCKS_PER_SEC = 50000000L;		//CONFREG时钟频率
unsigned long CORE_CLOCKS_PER_SEC = 33000000L;			//处理器核时钟频率

uint32_t data_input[16] = {
	0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd, 
	0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd,
	0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd, 
	0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd};
uint32_t result[8] = {
	0xfcb048ea, 0x84e4b7f9, 0x8a81f837, 0x63851a69,
	0x36b377d8, 0x3c884ce9, 0xabffda76, 0x260f520a};
uint32_t data_hash[8] = {0};
SM3_Handle_t s_handle;

int main(int argc, char** argv)
{
	printf("\nHello Loongarch32r SM3 INT!\n");

	uint32_t ret = 0;
	
	memset(&s_handle, 0x0, sizeof(SM3_Handle_t));
	sm3_init(&s_handle);
	for(int i = 0; i < 16; i++)
	{
		sm3_update(&s_handle, data_input[i]);
	}
	sm3_final_int(&s_handle);
	
	while(1)
	{

	}
	
	return 0;
}

uint32_t hash_call_back()
{
	sm3_callback(data_hash);
	if (memcmp(data_hash, result, 8) == 0) 
	{
        printf("SM3 success.\n");
    } else {
        printf("SM3 Faild.\n");
    }
	printf("SM3 INT test finished!\n");

	RegWrite(0xbf20f00c, 0x40);//clr
}


void HWI0_IntrHandler(void)
{	
	unsigned int int_state;
	int_state = RegRead(0xbf20f014);

	if((int_state) == 0x40){
		hash_call_back();
	}
}